using System;
using System.Collections.ObjectModel;
using System.Text;

namespace Doubler.Data
{

    public class MemberList : KeyedCollection<string, Reflector.CodeModel.IMemberDeclaration>
    {

        public new Boolean Contains(Reflector.CodeModel.IMemberDeclaration item)
        {
            return this.Contains(this.GetKeyForItem(item));
        }

        protected override string GetKeyForItem(Reflector.CodeModel.IMemberDeclaration item)
        {
            if (item is Reflector.CodeModel.IMethodDeclaration)
            {
                Reflector.CodeModel.IMethodDeclaration Method = (Reflector.CodeModel.IMethodDeclaration)item;
                StringBuilder KeyBuilder = new StringBuilder();
                KeyBuilder.Append("Method");
                KeyBuilder.Append(Method.Name);
                foreach (Reflector.CodeModel.IParameterDeclaration Parameter in Method.Parameters)
                {
                    KeyBuilder.Append(Parameter.ParameterType.ToString());
                }
                return KeyBuilder.ToString();
            }
            else if (item is Reflector.CodeModel.IPropertyDeclaration)
            {
                Reflector.CodeModel.IPropertyDeclaration Property = (Reflector.CodeModel.IPropertyDeclaration)item;
                StringBuilder KeyBuilder = new StringBuilder();
                KeyBuilder.Append("Property");
                KeyBuilder.Append(Property.Name);
                foreach (Reflector.CodeModel.IParameterDeclaration Parameter in Property.Parameters)
                {
                    KeyBuilder.Append(Parameter.ParameterType.ToString());
                }
                return KeyBuilder.ToString();
            }
            else if (item is Reflector.CodeModel.IFieldDeclaration)
            {
                Reflector.CodeModel.IFieldDeclaration Field = (Reflector.CodeModel.IFieldDeclaration)item;
                StringBuilder KeyBuilder = new StringBuilder();
                KeyBuilder.Append("Field");
                KeyBuilder.Append(Field.Name);
                return KeyBuilder.ToString();
            }
            else if (item is Reflector.CodeModel.IEventDeclaration)
            {
                Reflector.CodeModel.IEventDeclaration Event = (Reflector.CodeModel.IEventDeclaration)item;
                StringBuilder KeyBuilder = new StringBuilder();
                KeyBuilder.Append("Event");
                KeyBuilder.Append(Event.Name);
                if (Event.InvokeMethod != null)
                {
                    foreach (Reflector.CodeModel.IParameterDeclaration Parameter in Event.InvokeMethod.Resolve().Parameters)
                    {
                        KeyBuilder.Append(Parameter.ParameterType.ToString());
                    }
                }
                return KeyBuilder.ToString();
            }
            throw new InvalidOperationException(String.Format("Was not expecting what every type a {0} is.", item.Name));
        }
    }


}
